package com.myoffice.biz.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myoffice.biz.UserInfoBiz;
import com.myoffice.dao.BaseDaoI;
import com.myoffice.model.TLoginlog;
import com.myoffice.model.TUserinfo;
import com.myoffice.pageModel.DataGrid;
import com.myoffice.pageModel.UserInfo;
import com.myoffice.pageModel.base.Json;
import com.myoffice.pageModel.base.SessionInfo;

@Service("userInfoBiz")
public class UserInfoBizImpl implements UserInfoBiz {
	
	private BaseDaoI<TUserinfo> userDao=null;
	private BaseDaoI<TLoginlog> loginLogDao=null;
	
	public BaseDaoI<TUserinfo> getUserDao() {
		return userDao;
	}
	@Autowired
	public void setUserDao(BaseDaoI<TUserinfo> userDao) {
		this.userDao = userDao;
	}

	public BaseDaoI<TLoginlog> getLoginLogDao() {
		return loginLogDao;
	}
	@Autowired
	public void setLoginLogDao(BaseDaoI<TLoginlog> loginLogDao) {
		this.loginLogDao = loginLogDao;
	}

	public UserInfo login(UserInfo userInfo) {
		Map<String,Object> params= new HashMap<String,Object>();
		params.put("name", userInfo.getName());
		params.put("password", userInfo.getPassword());
		TUserinfo tUser=this.userDao.get("from TUserinfo t where t.name=:name and t.password=:password", params);
		if(tUser!=null){
			userInfo.setId(tUser.getId());
			return userInfo;
		}
	
		return null;
	}

	public UserInfo reg(UserInfo userInfo) {
		TUserinfo tUsers= new TUserinfo();
		
		tUsers=this.userDao.get("from TUserinfo t where t.name=?", new Object[]{userInfo.getName()});
		if(tUsers==null){
			TUserinfo tUser= new TUserinfo();
			tUser.setId(UUID.randomUUID().toString());
			tUser.setName(userInfo.getName());
			tUser.setPassword(userInfo.getPassword());
			tUser.setGender(userInfo.getGender());
			Serializable userId= this.userDao.save(tUser);
			if(!("".equals(userId))&&null!=userId){
				BeanUtils.copyProperties(tUser, userInfo);
				return userInfo;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}

	public void saveLoginLog(SessionInfo sessionInfo, Json j) {
		TUserinfo tuser=new TUserinfo();
		tuser=this.userDao.get(TUserinfo.class, sessionInfo.getUserId());
		TLoginlog tLoginLog= new TLoginlog();
		
		tLoginLog.setId(UUID.randomUUID().toString());
		tLoginLog.setTime(new Date());
		tLoginLog.setTUserinfo(tuser);
		if(j.isSuccess()){
			tLoginLog.setIfsuccess("1");	
		}else{
			tLoginLog.setIfsuccess("0");
		}
		
		tLoginLog.setUserip(sessionInfo.getIp());
		
		this.loginLogDao.save(tLoginLog);
		
	}
	public DataGrid datagrid(UserInfo userInfo) {
		DataGrid dg=new DataGrid();
		String hql="from TUserinfo t";
		Map<String,Object> params=new HashMap<String, Object>();
		hql = changeHql(userInfo, hql, params);
		String totalHql="select count(*)"+hql;
		hql = orderHql(userInfo, hql);
		List<TUserinfo> tul=userDao.find(hql,params,userInfo.getPage(),userInfo.getRows());
		List<UserInfo> nl=new ArrayList<UserInfo>();
		copyUser(tul, nl);
		dg.setTotal(userDao.count(totalHql,params));
		dg.setRows(nl);
		return dg;
	}
	private void copyUser(List<TUserinfo> tul, List<UserInfo> nl) {
		if(tul!=null&&tul.size()>0){
			for(TUserinfo t:tul){
				UserInfo u=new UserInfo();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
	}
	private String orderHql(UserInfo userInfo, String hql) {
		if(userInfo.getSort()!=null){
			hql+=" order by "+userInfo.getSort()+" "+userInfo.getOrder();
		}
		return hql;
	}
	private String changeHql(UserInfo userInfo, String hql,
			Map<String, Object> params) {
		if(userInfo.getName()!=null && !("").equals(userInfo.getName().trim())){
			hql+=" where  t.name like :name";
			params.put("name", "%%"+userInfo.getName().trim()+"%%");
		}
		return hql;
	}
	public void remove(String ids) {
		String [] nids=ids.split(",");
		String hql="delete TUserinfo t where t.id in (";
		for(int i=0; i<nids.length;i++){
			if(i>0){
				hql+=",";
			}
			hql+="'"+nids[i]+"'";
		}
		hql+=")";
		userDao.executeHql(hql);
	}
	public UserInfo edit(UserInfo userInfo) {
		TUserinfo tUser=userDao.get(TUserinfo.class, userInfo.getId());
		BeanUtils.copyProperties(userInfo,tUser,new String[]{"id","password"});
		return userInfo;
	}
}








